package db


import (
	_ "github.com/go-sql-driver/mysql"
)
// 获取新闻
type News struct {
	Id        	int	  `json:"id"`
	Title 		string		`json:"title"`
	Abstract	string		`json:"abstract"` //摘要
	ReleaseDate	string		`json:"releaseDate"`
	Content		string		`json:"content"`
	ContentText	string	`json:"contentText"`
	CoverSrc	string	`json:"coverSrc"`
	Cover		string	`json:"cover"`
	CoverId  	int		`json:"coverId"`
	IsTop  		int		`json:"isTop"`
	TextImgs	string  `json:"textImgs"`
}
func GetNews(page int,limit int)(as []News,err error){
	rows, err := ulwebsiteDb.Query(`
	SELECT
	    a.id,a.isTop,ifnull(a.title,""),ifnull(a.abstract,""),ifnull(a.releaseDate,""),ifnull(a.content,""),ifnull(a.contentText,""),ifnull(e.src,"") as coverSrc,ifnull(e.title,"") as cover,e.id as coverId,ifnull(a.textImgs,"")
    FROM
        article a
    LEFT JOIN 
		imgfile e on e.id = a.coverId 
	ORDER BY
		a.isTop desc,
		a.releaseDate desc
	LIMIT
		?,?
	`,page*limit,limit)
	if err != nil {
		return
	}
	defer rows.Close()
	as = make([]News,0)
	for rows.Next() {
		var a News
		err = rows.Scan(&a.Id,&a.IsTop,&a.Title,&a.Abstract,&a.ReleaseDate,&a.Content,&a.ContentText,&a.CoverSrc,&a.Cover,&a.CoverId,&a.TextImgs)
		if err != nil {
			return
		}
		as = append(as, a)
	}
	return
}
//单条新闻
func GetNewsDetail(id int)(as []News,err error){
	rows, err := ulwebsiteDb.Query(`
	SELECT
	    a.id,a.isTop,ifnull(a.title,""),ifnull(a.abstract,""),ifnull(a.releaseDate,""),ifnull(a.content,""),ifnull(a.contentText,""),ifnull(e.src,"") as coverSrc,ifnull(e.title,"") as cover,e.id as coverId,ifnull(a.textImgs,"")
    FROM
        article a
    LEFT JOIN 
		imgfile e on e.id = a.coverId 
	WHERE
		a.id = ?
	`,id)
	if err != nil {
		return
	}
	defer rows.Close()
	as = make([]News,0)
	for rows.Next() {
		var a News
		err = rows.Scan(&a.Id,&a.IsTop,&a.Title,&a.Abstract,&a.ReleaseDate,&a.Content,&a.ContentText,&a.CoverSrc,&a.Cover,&a.CoverId,&a.TextImgs)
		if err != nil {
			return
		}
		as = append(as, a)
	}
	return
}
//发布新闻
func PostNews(title string,abstract string,content string,contentText string,coverId string,isTop string,releaseDate string,textImgs string)(id int64,err error){
	tx, err := ulwebsiteDb.Begin()
	if err != nil {
		return
	}
	defer tx.Commit()

	//如果isTop == 1，将其他数据改为0
	if isTop == "1"{
		_, err := tx.Exec("UPDATE article SET isTop=0 ")
		if err != nil {
			tx.Rollback()
			return 0,err
		}
	}

	rows, err := tx.Exec("insert into article(title,abstract,content,contentText,coverId,isTop,releaseDate,textImgs) values(?,?,?,?,?,?,?,?)",title,abstract,content,contentText,coverId,isTop,releaseDate,textImgs)
	if err != nil {
		tx.Rollback()
		return
	}
	id,err = rows.LastInsertId()
	if err != nil {
		return
	}
	return
}
//修改新闻
func EditNews(title string,abstract string,content string,contentText string,coverId string,isTop string,uid string,textImgs string)(id int64,err error){
	tx, err := ulwebsiteDb.Begin()
	if err != nil {
		return
	}
	defer tx.Commit()

	//如果isTop == 1，将其他数据改为0
	if isTop == "1"{
		_, err := tx.Exec("UPDATE article SET isTop=0 ")
		if err != nil {
			tx.Rollback()
			return 0,err
		}
	}

	rows, err := tx.Exec("UPDATE article SET title=?,abstract=?,content=?,contentText=?,coverId=?,isTop=?,textImgs=? WHERE id=?",title,abstract,content,contentText,coverId,isTop,textImgs,uid)
	if err != nil {
		tx.Rollback()
		return
	}
	id,err = rows.LastInsertId()
	if err != nil {
		return
	}
	return
}
//删除新闻
func DelNews(uid string)(id int64,err error){
	tx, err := ulwebsiteDb.Begin()
	if err != nil {
		return
	}
	defer tx.Commit()

	//删除article
	rows, err := tx.Exec("DELETE FROM article WHERE id=?",uid)
	if err != nil {
		tx.Rollback()
		return
	}

	id,err = rows.LastInsertId()
	if err != nil {
		return
	}
	return
}